home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1958 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.5 KB

  1. Path: ix.netcom.com!netnews
  2. From: miker3@ix.netcom.com (Mike Rubenstein)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: What the hell is THIS?!
  5. Date: Thu, 18 Jan 1996 01:21:42 GMT
  6. Organization: Netcom
  7. Message-ID: <30fd9e14.22385664@nntp.ix.netcom.com>
  8. References: <4d6rgh$rfu@abel.cc.sunysb.edu> <coc-1301960253420001@dal1498.computek.net> <821539645snz@genesis.demon.co.uk> <4dk45c$pdp@gryphon.phoenix.net>
  9. NNTP-Posting-Host: ix-dc6-05.ix.netcom.com
  10. X-NETCOM-Date: Wed Jan 17  5:21:37 PM PST 1996
  11. X-Newsreader: Forte Agent .99c/16.141
  12.  
  13. brucew@phoenix.net (Bruce Wedding) wrote:
  14.  
  15. > Lawrence Kirby <fred@genesis.demon.co.uk> wrote:
  16. > >No, it is a pointer to an array of 3 ints.
  17. > Hi Lawrence,
  18. > I knew what it was, because I read AND understood that part of Deep C
  19. > Secrets.  What I don't understand is where one would need something
  20. > like this.  I can certainly use a pointer to an int array, but why
  21. > specifically an array of 3?  I don't think this buys you anything
  22. > really.  The three doesn't magically terminate the array like a nul.
  23. > It doesn't allocate any memory.  So why and where and what is wrong
  24. > with just allocating it, if that is what you need?
  25. > p = malloc(3 * sizeof(int));
  26.  
  27. The usual reason for this is to handle multidimensional arrays where
  28. all but the first dimension is known at compile time.  For example,
  29.  
  30.     int n;
  31.     int (*p)[3];
  32.     /* ... */
  33.     p = malloc(n * sizeof *p);
  34.  
  35. p can now be used as an array of [n][3] int (e.g., you can reference
  36. p[4][1] if 4 < n).
  37.  
  38.  
  39. Michael M Rubenstein
  40.